package com.guoxin.part1.chapter4;

import java.util.concurrent.TimeUnit;

/**
 * this monitor
 *
 * @author guoxin
 * @date 2019/8/27
 */
public class ThisMonitor {

    private synchronized void method1() {
        System.out.println(Thread.currentThread().getName() + " enter to method1");
        try {
            TimeUnit.MINUTES.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private synchronized void method2() {
        System.out.println(Thread.currentThread().getName() + " enter to method2");
        try {
            TimeUnit.MINUTES.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void method3() {
        synchronized (this) {
            System.out.println(Thread.currentThread().getName() + " enter to method2");
            try {
                TimeUnit.MINUTES.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        ThisMonitor tm = new ThisMonitor();
        new Thread(tm::method1).start();
        new Thread(tm::method2).start();
        new Thread(tm::method3).start();
    }
}
